//
//  JWTableViewCell.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/17.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWMovieCell.h"
#import "JWMovieCellModel.h"
#import "constant.h"
#import "Star.h"
#import "UIImageView+WebCache.h"



@interface JWMovieCell()
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *year;
@property (nonatomic, strong) UILabel *rate;
@property (nonatomic, strong) UIView *star;

@end
@implementation JWMovieCell

- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        
//      电影图片
        
        UIImageView *moviewImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        moviewImage.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:moviewImage];
        _image = moviewImage;
        
//      电影名
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:titleLabel];
        _title = titleLabel;
//        _title.backgroundColor = [UIColor grayColor];
        
//      电影评分
        
        UILabel *rateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//        rateLabel.textAlignment = NSTextAlignmentCenter;
        rateLabel.font = [UIFont systemFontOfSize:14];
        rateLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:rateLabel];
        _rate = rateLabel;
//        _rate.backgroundColor = [UIColor grayColor];
        
//      星级
        UIView *starView = [[UIView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:starView];
        _star = starView;
        

//      电影年份
        
        UILabel *yearLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//        yearLabel.textAlignment = NSTextAlignmentCenter;
        yearLabel.font = [UIFont systemFontOfSize:15];
        yearLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:yearLabel];
        _year = yearLabel;
//        _year.backgroundColor = [UIColor grayColor];

        
    }
    return self;
}
- (void)setMovieModel:(JWMovieCellModel *)movieModel{
    _movieModel = movieModel;
    
    //      电影图片
    
    NSString *urlStr = [_movieModel.images objectForKey:@"medium"];
    
//    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
//    UIImage *movieImage = [UIImage imageWithData:imageData];
//    self.imageView.image = movieImage;
    
    [self.image sd_setImageWithURL:[NSURL URLWithString:urlStr]];

    //      电影名
    
    self.title.text = _movieModel.title;

    //      电影评分

   // self.rate.text = [_movieModel.rating objectForKey:@"average"];//是NSnumber类型
    NSNumber *number = [_movieModel.rating objectForKey:@"average"];
    self.rate.text = [NSString stringWithFormat:@"%0.1f",[number floatValue]];
    
    //      电影星级
    
    Star *star = [[Star alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    star.max_star = 10;
    star.show_star = [number floatValue];
    star.font_size = 25;

    star.empty_color = [UIColor grayColor];
    star.full_color = [UIColor colorWithRed:255 / 255.f green:185 / 255.f blue:15 / 255.f alpha:1];
    
    [self.star addSubview:star];
    
    //      电影年份
   
    self.year.text = [NSString stringWithFormat:@"年份：%@", _movieModel.year];// year 是NSnumber类型
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat pading = (JWMovieCellHeight - 20 - 20 - 30) / 4; //间距
    
    self.image.frame = CGRectMake(10, 10, JWMovieImgW, JWMovieImgH);
    
    self.title.frame = CGRectMake(JWMovieImgW + 20, pading, 200, 20);
    
    self.rate.frame = CGRectMake(JWScreenWidth - 50 - 20, (JWMovieCellHeight - 20) * 0.5, 50, 20);
    
    self.star.frame = CGRectMake(JWMovieImgW + 20, pading * 2 + 20, 200, 30);
    
    self.year.frame = CGRectMake(JWMovieImgW + 20, JWMovieCellHeight - pading - 20 , 200, 20);

}

+ (instancetype)tableViewCellWithTableView:(UITableView *)table{
    
    static NSString *ID = @"movieCell";
    JWMovieCell *cell = [table dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[JWMovieCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
